package ca.llsutherland.squash.configuration;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;

import org.apache.commons.lang.StringUtils;

import ca.llsutherland.squash.persistence.AbstractDatabaseConnection;


public class SquashLadderConfiguration {

    private Properties properties = null;
    private static final String DB_URL = "db.url";
    private static final String DB_USERNAME = "db.username";
    private static final String DB_PASSWORD = "db.password";
    private static final String DATABASE_CONNECTION = "database.connetion";
    private static final String DB_DRIVER = "db.driver";
    
    private static SquashLadderConfiguration instance = new SquashLadderConfiguration();

    public static SquashLadderConfiguration getInstance() {
        return instance;
    }

    public Properties getProperties() {
        return properties;
    }

    public SquashLadderConfiguration() {
        this.properties = new Properties();
        loadDefaultProperties();
    }
    
    private void loadDefaultProperties() {
        String defaultPropertiesFileName = "SquashLadder.properties";
        if(!loadPropertiesFromResource(defaultPropertiesFileName)){
            //do nothing, i guess
        }
    }
    
    private boolean loadPropertiesFromResource(String defaultPropertiesFileName) {
        boolean resourceLoaded = false;
        try{
            InputStream resourceAsStream = SquashLadderConfiguration.class.getResourceAsStream(defaultPropertiesFileName);
            if(resourceAsStream != null){
                this.properties.load(resourceAsStream);
                resourceLoaded = true;
            }
        }catch(IOException ex){
            //huh?
        }
        return resourceLoaded;
    }
    
    public String getDatabaseUrl() {
        return this.properties.getProperty(DB_URL);
    }

    public String getDbUsername() {
        return this.properties.getProperty(DB_USERNAME);
    }

    public String getDbPassword() {
        return this.properties.getProperty(DB_PASSWORD);
    }

    public Properties getConfiguration() {
        return this.properties;
    }

    public void setConfiguration(Properties properties) {
        this.properties = properties;
    }
    
    public AbstractDatabaseConnection getDatabaseConnection() throws InstantiationException, IllegalAccessException, ClassNotFoundException {
        String databaseConnectionConfigurationSetting = this.properties.getProperty(DATABASE_CONNECTION);
        Object newInstance = Class.forName(databaseConnectionConfigurationSetting).newInstance();
        return (AbstractDatabaseConnection)newInstance;        
    }

    public String getDatabaseDriver() {
        return this.properties.getProperty(DB_DRIVER);
    }
    
    public List<String> getAllSortedCourtStyles(){
        return getFileNamesFromProperty("court.styles");
    }
    
    public List<String> getAllSortedRoles(){
        return getFileNamesFromProperty("roles");
    }
    
    private List<String> getFileNamesFromProperty(String property) {
        String filenames = this.properties.getProperty(property);
        String[] filenameArray = StringUtils.split(filenames, ";,");

        if (filenameArray != null) {
            return Arrays.asList(filenameArray);
        } else {
            return new ArrayList<String>();
        }
    }
    
}
